// import { _decorator, Component, Node, PhysicsSystem, game, sys, find, director } from 'cc';

const tt = window['tt']||window['wx'];
let bannerAd:any = null;
let customAdRight:any = null;
let customAdLeft:any = null;
let interstitialAd:any = null;

export function showVideo(success:any, fail:any) {

    if (!tt) {
        console.log("不是微信抖音环境")
        success&&success()
        return 
    }
    console.log('触发视频广告')

    let videoad = tt.createRewardedVideoAd({ adUnitId: "11qhdojmj368604vb6" });
    videoad.onClose((res) => {
        console.log('video on close', res);
        if (res.isEnded) {
            success && success();
        } else {
            showToast('需要完整观看视频');
            fail && fail()
        }
        console.log('onClose销毁视频广告')
        // videoad.destroy();


    });
    videoad.onError((err) => {
        console.log('onError销毁视频广告')
        // videoad.destroy()
        // fail && fail();
        success && success();
        console.log('videoadfail', err)
        showToast('暂无广告，稍后重试');
    });
    videoad.onLoad(() => {
        console.log('视频广告加载成功')
    });

    videoad.show().catch(() => {
        videoad.load().then(() => videoad.show()).catch(err => {
            console.log('load')
            console.log(err)
        });

    });
}

export function createInterstitialAd() {
    // 在适合的场景显示插屏广告
    console.log('显示插屏广告')
        interstitialAd = tt.createInterstitialAd({
            adUnitId: "1j66nga7w9ui63624e"
        })

        interstitialAd.onError((res) => {
            console.log("插屏onError", res);
        });

        interstitialAd.show().catch((err) => {
            console.log(err);
        })

}
export function showInterstitialAd() {
    if (interstitialAd) {
        interstitialAd.show().catch((err) => {
            console.log(err);
        })
    }
    else{
        this.createInterstitialAd();
    }
   
}

export function destroyInterstitialAd(){
    if (interstitialAd) {
    interstitialAd.destroy();
    }
}


//显示Custom广告 显示格子广告左边

export function showCustomAdLeft() {
    console.log("显示格子广告左边")
    if (customAdLeft) {
        customAdLeft.show().catch((err) => {
            console.log('显示左边格子广告失败', err);
        })
        return
    }
    else{
            customAdLeft = tt.createCustomAd({
                adUnitId: GameMgr_my.ins.httpData.ads.customId1,
                style: {
                    left: 40,
                    top: 30,
                    width:0.1,
                    fixed: true
                }
            })
            customAdLeft.onError(err => {
                console.error(err.errMsg)
            });
            customAdLeft.show().catch((err) => {
                console.log('显示左边格子广告失败', err);
            })
    }
    

}

export function hideCustomAdLeft() {
    if (customAdLeft) {
        customAdLeft.hide().catch((err) => {
            console.log('隐藏左边格子广告失败', err);
        })
    }
    
}
export function destroyCustomAdLeft() {
    if (customAdLeft) {
    customAdLeft.destroy();
        
    }
}


export function showCustomAdRight() {
    console.log("显示格子广告右边")

    if (customAdRight) {
        customAdRight.show().catch((err) => {
            console.log('显示右边格子广告失败', err);
        })
        return
    }
    else{
            const winSize = tt.getSystemInfoSync();
            customAdRight = tt.createCustomAd({
                adUnitId: GameMgr_my.ins.httpData.ads.customId2,
                style: {
                    left: winSize.windowWidth - 80,
                    top: 30,
                    width:0.3,
                    fixed: true
                }
            })
            customAdRight.onError(err => {
                console.error(err.errMsg)
            });
            customAdRight.show().catch((err) => {
                console.log('显示右边格子广告失败', err);
            })
    }
    
}


export function hideCustomAdRight() {
    if (customAdRight) {
        customAdRight.hide().catch((err) => {
            console.log('隐藏右边格子广告失败', err);
        })
    }
    
}

export function destroyCustomAdRight() {
    if (customAdRight) {
    customAdRight.destroy();
        
    }
}


export function showToast(str) {
    // if (!tt) {
    //     console.log(' show toast ', str)
    //     return
    // }
    tt.showToast({
        title: str,
        duration: 2000,
        icon: 'none',
        success(res) {
            console.log(`${res}`);
        },
        fail(res) {
            console.error(`showToast调用失败`);
        },
    });
}


export function showBanner() {
    if (bannerAd) {
        bannerAd.show().catch((err) => {
            console.log('显示广告失败', err);
        })
        return
    }
    else{
        const winSize = tt.getSystemInfoSync();
    
        //创建 Banner 广告实例，提前初始化 
        bannerAd = tt.createBannerAd({ adUnitId: GameMgr_my.ins.httpData.ads.bannerId, style: { left: (winSize.windowWidth - 350) / 2, top: winSize.windowHeight - 100, width: 350 } })
    
        // 监听 banner 广告错误事件 
        bannerAd.onError(err => { console.error(err.errMsg) });
        bannerAd.show().catch((err) => {
            console.log('显示广告失败', err);
        })
    }
   
}

export function hideBanner() {
    if (bannerAd) {
        bannerAd.hide().catch((err) => {
            console.log('隐藏广告失败', err);
        })
    }
   
}

export function destroyBanner() {
    if (bannerAd) {
        bannerAd.destroy();
    }
}



